package com.aaa.controller;

import com.aaa.entity.MyReturn;
import com.aaa.entity.Test_plan;
import com.aaa.entity.Tester;
import com.aaa.entity.TesterLogin;
import com.aaa.service.Test_plan_fpaService;
import com.aaa.service.TesterFPAService;
import com.aaa.service.TesterSASService;
import com.aaa.service.TesterSDSService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("testerLogin")
public class TesterLoginController extends BaseController{

    @Autowired
    private Test_plan_fpaService fpaService;
    @Autowired
    private TesterFPAService testerFPAService;
    @Autowired
    private TesterSASService testerSASService;
    @Autowired
    private TesterSDSService testerSDSService;

    MyReturn myReturn=new MyReturn();


    /**
    * @Author: WTH
    * @Description: 用户测试登陆
    * @Date:2022-07-07 15:08
    * @Params:
    */
    @ResponseBody
    @RequestMapping("userTestLogin.do")
    public MyReturn selectTestType(TesterLogin testerLogin, HttpSession session){
        //获取邀请码
        Integer test_code = testerLogin.getTest_code();

        //根据获取到的邀请码去表中进行查询
        Test_plan test_plan = fpaService.selectByCode(test_code);
        Integer id = test_plan.getId();

        //将测试人信息存入表中
        Tester tester=new Tester();
        tester.setName(testerLogin.getName());
        tester.setPhone(testerLogin.getPhone());
        tester.setCreate_time(new Date());
        tester.setTest_plan_id(id);
        session.setAttribute("tester",tester);


        //不为空说明存在测试，判断是哪种测试
        if(test_plan!=null){
            //判断测试类型
            switch (test_plan.getTest_type()){
                case "FPA性格测试":{
                    //根据手机号查此用户是否做过测试
                    List<Tester> testers = testerFPAService.selectTesterByPhone(tester);
                    //判断不为空说明已测试过跳转至结果页
                    if(testers!=null && testers.size()>0){
                        System.out.println("跳转测试结果页");
                        return super.getFailResult(myReturn);
                    }
                    //判断为空，未测试，将用户添加至测试人员表内
                    else{
                        testerFPAService.addTester(tester);
                        session.setAttribute("tester",tester);
                        myReturn.setData("/question/questionAllfpa.do");
                        return super.getSuccessResult(myReturn);
                    }
                }
                case "SAS测试":{
                    //根据手机号查询
                    List<Tester> testers = testerSASService.selectTesterByPhone(tester);
                    //判断是否为空
                    if(testers!=null && testers.size()>0){
                        System.out.println("跳转结果页");
                        return super.getFailResult(myReturn);
                    }else{
//                        testerSASService.addTester(tester);
//                        session.setAttribute("tester",tester);
                        myReturn.setData("/QTSAS/questionAllSas.do");
                        return super.getSuccessResult(myReturn);
                    }
                }
                case "SDS测试":{
                    //根据手机号查询
                    List<Tester> testers = testerSDSService.selectTesterByPhone(tester);
                    //判断是否为空
                    if(testers!=null && testers.size()>0){
                        System.out.println("跳转结果页");
                        return super.getFailResult(myReturn);
                    }else{
                        testerSDSService.addTester(tester);
                        session.setAttribute("tester",tester);
                        myReturn.setData("/QTSDS/questionAllSds.do");
                        return super.getSuccessResult(myReturn);
                    }
                }
                //没有这个邀请码
                default:{
                    return super.getFailResult(myReturn);
                }
            }
        }
        return super.getFailReturn(myReturn);
    }
}
